package com.ruoyi.gd.gdzlkFl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gd.gdzlkFl.domain.bo.ZGdzlkFlBo;
import com.ruoyi.gd.gdzlkFl.domain.vo.ZGdzlkFlVo;
import com.ruoyi.gd.gdzlkFl.domain.ZGdzlkFl;
import com.ruoyi.gd.gdzlkFl.mapper.ZGdzlkFlMapper;
import com.ruoyi.gd.gdzlkFl.service.IZGdzlkFlService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工单资料库分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-21
 */
@RequiredArgsConstructor
@Service
public class ZGdzlkFlServiceImpl implements IZGdzlkFlService {

    private final ZGdzlkFlMapper baseMapper;

    /**
     * 查询工单资料库分类
     */
    @Override
    public ZGdzlkFlVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工单资料库分类列表
     */
    @Override
    public TableDataInfo<ZGdzlkFlVo> queryPageList(ZGdzlkFlBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZGdzlkFl> lqw = buildQueryWrapper(bo);
        Page<ZGdzlkFlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工单资料库分类列表
     */
    @Override
    public List<ZGdzlkFlVo> queryList(ZGdzlkFlBo bo) {
        LambdaQueryWrapper<ZGdzlkFl> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZGdzlkFl> buildQueryWrapper(ZGdzlkFlBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZGdzlkFl> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getFlbm()), ZGdzlkFl::getFlbm, bo.getFlbm());
        lqw.like(StringUtils.isNotBlank(bo.getFlname()), ZGdzlkFl::getFlname, bo.getFlname());
        lqw.eq(StringUtils.isNotBlank(bo.getBz()), ZGdzlkFl::getBz, bo.getBz());
        lqw.eq(StringUtils.isNotBlank(bo.getQx()), ZGdzlkFl::getQx, bo.getQx());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateuserid()), ZGdzlkFl::getCreateuserid, bo.getCreateuserid());
        lqw.orderByAsc(ZGdzlkFl::getPx);
        return lqw;
    }

    /**
     * 新增工单资料库分类
     */
    @Override
    public Boolean insertByBo(ZGdzlkFlBo bo) {
        ZGdzlkFl add = BeanUtil.toBean(bo, ZGdzlkFl.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工单资料库分类
     */
    @Override
    public Boolean updateByBo(ZGdzlkFlBo bo) {
        ZGdzlkFl update = BeanUtil.toBean(bo, ZGdzlkFl.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZGdzlkFl entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工单资料库分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
